package com.lu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.lu.pojo.AdminUser;
import com.lu.dao.AdminUserMapper;
import com.lu.service.AdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 后台管理员信息表 服务实现类
 * </p>
 *
 * @author xiaolu
 * @since 2020-05-27
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    // 验证密码
    @Override
    public boolean validatePassword(Integer userId, String oldPwd) {

        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>(
                new AdminUser().setAdminUserId(userId)
                        .setLoginPassword(MD5Utils.MD5Encode(oldPwd, "UTF-8"))
        );
        AdminUser adminUser = adminUserMapper.selectOne(queryWrapper);
        // 如果为空-->没找到(密码错误) 返回1 --> ！ 返回0
        return !StringUtils.isEmpty(adminUser);
    }

    @Transactional // 声明式事务管理
    @Override
    public boolean updateUserInfo(AdminUser adminUser) {

        // 判断更新数据库操作是否成功
        return SqlHelper.retBool(adminUserMapper.updateById(adminUser));
    }
}
